package com.nema.servlet;


import com.nema.util.QueryByTitle;
import lsh.LshPackage.Pair;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Servlet implementation class InitQuery
 */
@WebServlet("/InitQuery")
public class InitQuery extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitQuery() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        // 接收数据
        String title = request.getParameter("title");
        QueryByTitle qbt = new QueryByTitle();
        Map<String, Object> info = qbt.query(title);

        Pair[] pairs = (Pair[]) info.get("pairs");
        request.getSession().setAttribute("pairs",pairs);
        
        request.setAttribute("info",info);      
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("search.jsp");
        requestDispatcher.forward(request,response);
        
      /*  PrintWriter out = response.getWriter();
        out.print("<script>alert('"+info.get("country")+"');history.go(-1);</script>");
        out.flush();
        out.close();*/
	}

}







